
#include <BFile>
#include <BXmlDocument>

#include "Settings.h"

Settings::Settings()
{
	_dirty = false;

	_showTabs = false;
	_showSpaces = false;
	_showReturns = false;
	_showLinefeeds = false;
}
Settings::~Settings()
{

}

bool Settings::load()
{
	BString fileName = this->name();
	BFile file(fileName);
	if (!file.open(IO_Read))
	{
		return false;
	}

	if (fileName.endWith(".xml"))
	{
		BXmlDocument xmldoc;
		BXmlNode* root = xmldoc.read(file);
		if (!root)
		{
			return false;
		}
		if (BXmlNode* node = root->child("language"))
		{
			_language = node->text();
		}
		if (BXmlNode* node = root->child("theme-style"))
		{
			_themeStyle = node->text();
		}
		if (BXmlNode* node = root->child("show-tabs"))
		{
			_showTabs = node->text() == "true";
		}
		if (BXmlNode* node = root->child("show-spaces"))
		{
			_showSpaces = node->text() == "true";
		}
		if (BXmlNode* node = root->child("show-returns"))
		{
			_showReturns = node->text() == "true";
		}
		if (BXmlNode* node = root->child("show-linefeed"))
		{
			_showLinefeeds = node->text() == "true";
		}
		_extentionTable.clear();
		if (BXmlNode* node = root->child("extention-table"))
		{
			_extentionTable.resize(node->childCount(), 2);
			for (int i = 0; i < node->childCount(); i++)
			{
				BXmlNode* item = node->child(i);
				_extentionTable.set(i, 0, item->attrib("extention"));
				_extentionTable.set(i, 1, item->attrib("style"));
			}
		}
		_dirty = false;
		return true;
	}

	return false;
}
bool Settings::save()
{
	BString fileName = this->name();
	BFile file(fileName);
	if (!file.open(IO_Write))
	{
		return false;
	}

	BXmlDocument xmldoc;
	xmldoc.setFormat("/>\n");
	BXmlNode* root = new BXmlNode("settings");
	root->setFormat(">\n</>\n");
	if (BXmlNode* node = root->addChild("language"))
	{
		node->setText(_language);
	}
	if (BXmlNode* node = root->addChild("theme-style"))
	{
		node->setText(_themeStyle);
	}
	if (BXmlNode* node = root->addChild("show-tabs"))
	{
		node->setText(_showTabs ? "true" : "false");
	}
	if (BXmlNode* node = root->addChild("show-spaces"))
	{
		node->setText(_showSpaces ? "true" : "false");
	}
	if (BXmlNode* node = root->addChild("show-returns"))
	{
		node->setText(_showReturns ? "true" : "false");
	}
	if (BXmlNode* node = root->addChild("show-linefeed"))
	{
		node->setText(_showLinefeeds ? "true" : "false");
	}
	if (BXmlNode* node = root->addChild("extention-table"))
	{
		node->setFormat(">\n</>\n");
		for (int i = 0; i < _extentionTable.row(); i++)
		{
			BXmlNode* item = node->addChild("item");
			item->setAttrib("extention", _extentionTable.at(i, 0));
			item->setAttrib("style", _extentionTable.at(i, 1));
		}
	}
	bool res = xmldoc.write(file, root);
	_dirty = false;
	return true;
}

void Settings::dirty() const
{
	_dirty = true;
}
bool Settings::isDirty() const
{
	return _dirty;
}

void Settings::setLanguage(const BString& language)
{
	if (_language != language)
	{
		_language = language;
		_dirty = true;
		emit("language-changed", language);
	}
}
const BString& Settings::language() const
{
	return _language;
}

void Settings::setThemeStyle(const BString& themeStyle)
{
	if (_themeStyle != themeStyle)
	{
		_themeStyle = themeStyle;
		_dirty = true;
	}
}
const BString& Settings::themeStyle() const
{
	return _themeStyle;
}

void Settings::setShowTabs(bool show)
{
	if (_showTabs != show)
	{
		_showTabs = show;
		_dirty = true;
	}
}
bool Settings::showTabs() const
{
	return _showTabs;
}

void Settings::setShowSpaces(bool show)
{
	if (_showSpaces != show)
	{
		_showSpaces = show;
		_dirty = true;
	}
}
bool Settings::showSpaces() const
{
	return _showSpaces;
}

void Settings::setShowReturns(bool show)
{
	if (_showReturns != show)
	{
		_showReturns = show;
		_dirty = true;
	}
}
bool Settings::showReturns() const
{
	return _showReturns;
}

void Settings::setShowLinefeeds(bool show)
{
	if (_showLinefeeds != show)
	{
		_showLinefeeds = show;
		_dirty = true;
	}
}
bool Settings::showLinefeeds() const
{
	return _showLinefeeds;
}

void Settings::setColorGroup(const BString& type, const ColorGroup& colorGroup)
{
	_colorGroupMap[type] = colorGroup;
}
const ColorGroup& Settings::colorGroup(const BString& type)
{
	return _colorGroupMap(type);
}

void Settings::setColor(const BString& type, const BString& name, const BColor& color)
{
	_colorGroupMap[type][name] = color;
}
const BColor& Settings::color(const BString& type, const BString& name) const
{
	return _colorGroupMap(type)(name);
}

bool Settings::assignColor(const BString& type, const BString& name, BColor& color)
{
	auto git = _colorGroupMap.find(type);
	if (git.empty())
		return false;
	auto it = git->find(name);
	if (it.empty())
		return false;
	color = *it;
	return true;
}

void Settings::setExtentionTable(const StringTable& extentionTable)
{
	if (_extentionTable != extentionTable)
	{
		_extentionTable = extentionTable;
		_dirty = true;
	}
}
const StringTable& Settings::extentionTable() const
{
	return _extentionTable;
}
